package com.example.demo.config.filter;

import java.io.IOException;
import java.time.Instant;
import java.util.UUID;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.example.demo.config.consts.LogConst;
import com.example.demo.config.logconfig.LogBack;
import com.example.demo.zhoushengwen.utils.RequestUtil;

/**
 * 
 * 过滤器定义
 * request装饰类
 * 
 * @author  huxing
 * @version  [版本号, 2020年3月18日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */

@Component
@WebFilter(filterName = "bodyReaderFilter", urlPatterns = "/*")
@Order(value = 1)
public class BodyReaderFilter implements Filter
{
    @Autowired
    private RequestUtil requestUtil;
    
    @Override
    public void init(FilterConfig filterConfig)
        throws ServletException
    {
        // do nothing
    }
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException
    {
        long begin = System.currentTimeMillis();
        HttpServletRequest servletRequest = (HttpServletRequest)request;
        HttpServletResponse servletResponse = (HttpServletResponse)response;
        //设置返回信息头部信息，用来计算接口响应速度及查询日志
        servletResponse.setHeader(LogConst.LOG_UUID, UUID.randomUUID().toString());
        servletResponse.setHeader(LogConst.HANDLE_BEGIN, String.valueOf(Instant.now().toEpochMilli()));
        String requestParam = null;
        ServletRequest requestWrapper = null;
        if (request instanceof HttpServletRequest)
        {
            //使用装饰类，保证流的重复利用
            BodyReaderHttpServletRequestWrapper rWrapper = new BodyReaderHttpServletRequestWrapper(servletRequest);
            rWrapper.setHeader(LogConst.LOG_UUID, servletResponse.getHeader(LogConst.LOG_UUID));
            requestWrapper = rWrapper;
            requestParam = requestUtil.getRequestBody(rWrapper);
        }
        LogBack.info("CLIENTIP:{}|UUID:{}|position:{}|level:{}|status:{}|method:{}|param:{}|consume:{}",
            RequestUtil.getCliectIp(servletRequest),
            servletResponse.getHeader(LogConst.LOG_UUID),
            LogConst.LOG_FILTER,
            LogConst.LOG_STATUS_INFO,
            LogConst.LOG_BEGIN,
            servletRequest.getRequestURL().toString(),
            requestParam,
            0);
        
        if (requestWrapper == null)
        {
            chain.doFilter(servletRequest, servletResponse);
        }
        else
        {
            chain.doFilter(requestWrapper, servletResponse);
        }
        LogBack.info("CLIENTIP:{}|UUID:{}|position:{}|level:{}|status:{}|method:{}|param:{}|consume:{}",
            RequestUtil.getCliectIp(servletRequest),
            servletResponse.getHeader(LogConst.LOG_UUID),
            LogConst.LOG_FILTER,
            LogConst.LOG_STATUS_INFO,
            LogConst.LOG_END,
            servletRequest.getRequestURL().toString(),
            servletResponse.getHeader(LogConst.HANDLE_RESULT),
            (System.currentTimeMillis() - begin));
    }
    
    @Override
    public void destroy()
    {
        
    }
}
